package com.mk.property.controller;

import com.aliyuncs.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mk.property.common.R;
import com.mk.property.dto.ChargeDto;
import com.mk.property.pojo.Owner;
import com.mk.property.pojo.Room;
import com.mk.property.service.OwnerService;
import com.mk.property.service.RoomService;
import com.mk.property.vo.ChargeFormVO;
import com.mk.property.vo.RoomVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/room")
public class RoomController {
    @Autowired
    RoomService roomService;

    @Autowired
    OwnerService ownerService;

    /**
     * 分页查询
     *
     * @param page       当前查询页码
     * @param pageSize   每页展示的记录数
     * @param roomNumber 房号
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String roomNumber) {
        log.info("page = {},pageSize = {},roomNumber = {}", page, pageSize, roomNumber);
        //构造分页构造器
        Page<Room> pageInfo = new Page(page, pageSize);
        Page<RoomVO> roomVOPage = new Page<>();

        //条件构造器
        LambdaQueryWrapper<Room> lqw = new LambdaQueryWrapper<>();
        //name不为空才会执行下面的语句
        lqw.like(StringUtils.isNotEmpty(roomNumber), Room::getRoomNumber, roomNumber);
        //执行查询
        roomService.page(pageInfo, lqw);
        //对象拷贝
        BeanUtils.copyProperties(pageInfo, roomVOPage, "records");
        List<Room> records = pageInfo.getRecords();

        List<RoomVO> list = records.stream().map((item) -> {
            RoomVO roomVO = new RoomVO();
            BeanUtils.copyProperties(item, roomVO);
            Long ownerId = item.getOwnerId();
            Owner owner = ownerService.getById(ownerId);
            if (owner != null) {
                String ownerName = owner.getName();
                roomVO.setOwnerName(ownerName);
            }
            return roomVO;
        }).collect(Collectors.toList());

        roomVOPage.setRecords(list);
        return R.success(roomVOPage);
    }

    /**
     * 添加房屋信息
     *
     * @param room
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Room room) {
        log.info("新增房屋,房屋信息为{}", room.toString());
        roomService.save(room);
        return R.success("新增房屋成功");
    }

    /**
     * 根据id查询房屋信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Room> getById(@PathVariable Long id) {
        log.info("根据id查询房屋信息");
        Room room = roomService.getById(id);
        if (room != null) {
            return R.success(room);
        }
        return R.error("没有查询到对应房屋信息");
    }

    @PutMapping
    public R<String> update(@RequestBody Room room) {
        log.info(room.toString());
        roomService.updateById(room);
        return R.success("房屋信息修改成功");
    }

    /**
     * 由前端传来的信息获取收费单
     *
     * @param chargeDto
     * @return
     */
    @PostMapping("/chargeInfo")
    public R<List<ChargeFormVO>> getChargeInfo(@RequestBody ChargeDto chargeDto) {
        log.info(chargeDto.toString());
        List<ChargeFormVO> list = roomService.getChargeInfo(chargeDto);
        return R.success(list);
    }

    /**
     * 把前端传来的数据转换成订单保存到数据库中
     * 需要操作两个表
     * charge
     * room
     * @param chargeDto
     * @return
     */
    @PostMapping("/chargeOrder")
    public R<String> submitChargeOrder(@RequestBody ChargeDto chargeDto) {
        log.info(chargeDto.toString());
        roomService.submitChargeOrder(chargeDto);
        return R.success("支付成功!");
    }
}
